package com.esunrising.miniodemo.controller;

import com.esunrising.miniodemo.dto.MinioObject;
import com.esunrising.miniodemo.dto.Result;
import com.esunrising.miniodemo.util.MinioUtil;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/index")
public class MinioController {

    @Autowired
    MinioUtil minioUtil;

    @GetMapping("/index")
    public String index(String param){
        return "Hello,"+param;
    }
    @PostMapping("/upload")
    public Result<Void> upload(@RequestParam(value = "bucketName", required = false) String bucketName,
                       @RequestParam(value = "dirPath", required = false) String dirPath,
                       @RequestParam(name = "multipartFile") MultipartFile multipartFile) {
        //String bucketName = "test0811";
        String filePath = multipartFile.getOriginalFilename();
        if(dirPath != null && dirPath.length() > 0){
            filePath = dirPath + "/" + filePath;
        }
        try {
            minioUtil.uploadFile(bucketName,filePath,multipartFile);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.ok();
    }

    /**
     * 获取minio中的文件，直接打开浏览器文件下载对话框
     * @param response
     */
    @GetMapping("/get")
    public void getFile(HttpServletResponse response) {
        String bucketName = "test0811";
        String filePath = "f1080.sql";
        try {
            minioUtil.downloadFile(bucketName, filePath, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取minio中的文件，直接打开浏览器文件下载对话框
     * @param response
     */
    @GetMapping("/downloadFile")
    public void downloadFile(@RequestParam(value = "bucketName", required = false) String bucketName,
                             @RequestParam(value = "filePath", required = false) String filePath,
                             HttpServletResponse response) {
//        String bucketName = "test0811";
//        String filePath = "f1080.sql";
        try {
            minioUtil.downloadFile(bucketName, filePath, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/removeObject")
    public Result<Void> removeObject(@RequestParam(value = "bucketName", required = false) String bucketName,
                                     @RequestParam(value = "filePath", required = false) String filePath){

        try {
            minioUtil.removeObject(bucketName, filePath);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("发生错误，删除失败");
        }

    }

    @GetMapping("/listBuckets")
    public Result<List<String>> listBuckets(){
        try {
            List<Bucket> buckets = minioUtil.listBucket();
            List<String> list = new ArrayList<>();
            for (Bucket bucket : buckets) {
                list.add(bucket.name());
            }
            return Result.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok(null);
        }
    }

    @GetMapping("/listObject")
    public Result<List<MinioObject>> listObject(@RequestParam(value = "bucketName", required = true) String bucketName){
        try {
            List<MinioObject> objects = minioUtil.listObject(bucketName);
            return Result.ok(objects);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok(null);
        }
    }
    @GetMapping("/listObjectLayui")
    public Result<List<MinioObject>> listObjectLayui(@RequestParam(value = "bucketName", required = true) String bucketName){
        if(bucketName == null || bucketName.length() == 0){
            return Result.ok(0,"请选择Bucket", null);
        }
        try {
            List<MinioObject> objects = minioUtil.listObject(bucketName);
            return Result.ok(0,"成功", objects);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok(null);
        }
    }
}
